#region  == Copyright == 
// =====================================================================
//  Microsoft Consulting Services France - Aymeric Mouillé - 2013
//  Projet     : CRM Solution Management Tool
//  Fichier    : SettingsControl.cs (30/04/2013 16:49)
//  
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// =====================================================================
#endregion

namespace CRM.Solution.Management.Tool.Controls
{
    #region  == Using == 
    using System.Windows.Forms;

    using CRM.Solution.Management.Tool;
    using CRM.Solution.Manager.Properties;

    #endregion

    /// <summary>
    /// </summary>
    public partial class SettingsControl : UserControl
    {
        /// <summary>
        /// </summary>
        private Parameters parameters;

        #region Main Form
        /// <summary>
        /// Gets the main form.
        /// </summary>
        /// <value>
        /// The main form.
        /// </value>
        private MainForm MainForm
        {
            get
            {
                return (MainForm)Parent.Parent.Parent;
            }
        }
        #endregion

        #region = Constructor =
        /// <summary>
        /// Initializes a new instance of the <see cref="SettingsControl" /> class.
        /// </summary>
        public SettingsControl()
        {
            InitializeComponent();
        }
        #endregion

        #region Get Crm Connection String
        /// <summary>
        /// Gets the CRM connection string.
        /// </summary>
        /// <returns></returns>
        public string GetCrmConnectionString()
        {
            if (!IsSettingsDefined())
            {
                return string.Empty;
            }
            if (!string.IsNullOrEmpty(Settings.Default.CrmConnectionString))
            {
                return Settings.Default.CrmConnectionString;
            }
            return string.Format("Url={0}; Domain={1}; Username={2}; Password={3}", Settings.Default.OrgUrl, Settings.Default.Domain, Settings.Default.Login, Settings.Default.Password);
        }
        #endregion

        #region Is Settings Defined
        /// <summary>
        /// Determines whether [is settings defined].
        /// </summary>
        /// <returns>
        ///     <c>true</c> if [is settings defined]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsSettingsDefined()
        {
            if (string.IsNullOrEmpty(Settings.Default.OrgUrl))
            {
                MessageBox.Show(Resources.MainForm_GetCrmConnectionString_You_need_do_specify__organization_url__in_settings_tab);
                return false;
            }

            if (string.IsNullOrEmpty(Settings.Default.Domain))
            {
                MessageBox.Show(Resources.MainForm_GetCrmConnectionString_You_need_do_specify__domain__in_settings_tab);
                return false;
            }

            if (string.IsNullOrEmpty(Settings.Default.Login))
            {
                MessageBox.Show(Resources.MainForm_GetCrmConnectionString_You_need_do_specify__login__in_settings_tab);
                return false;
            }

            if (string.IsNullOrEmpty(Settings.Default.Password))
            {
                MessageBox.Show(Resources.MainForm_GetCrmConnectionString_You_need_do_specify__password__in_settings_tab);
                return false;
            }
            return true;
        }
        #endregion

        #region Load Control
        /// <summary>
        /// Loads the control.
        /// </summary>
        public void LoadControl()
        {
            parameters = new Parameters();
            propertyGrid.SelectedObject = parameters;
        }
        #endregion

        #region Property Grid Property Value Changed
        /// <summary>
        /// </summary>
        /// <param name="s">
        /// </param>
        /// <param name="e">
        /// </param>
        private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            parameters.Save();
            if (IsSettingsDefined())
            {
                MainForm.OrganizationService = MainForm.GetOrganizationService();
            }
        }
        #endregion
    }
}
